if (!cc.loader.loadResAll) {

    cc.loader.loadResAll = cc.loader.loadResDir;
}

cc.Class({
    extends: cc.Component,

    properties: {
        tipLabel: cc.Label,
        progressBar: cc.ProgressBar,
        _stateStr: '',
        _progress:0.0,
        _isLoading: false,

    },

    // use this for initialization
    onLoad: function () {
        if (!cc.sys.isNative && cc.sys.isMobile) {
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }

        this.tipLabel.string = this._stateStr;
        this.startPreloading();
    },

    startPreloading: function () {
        console.log("正在加载资源，请稍候");
        this._stateStr = "正在加载资源，请稍候";


        this._isLoading = true;
        var self = this;


        cc.loader.loadResAll('textures',(completedCount, totalCount, item)=>{
            if (self._isLoading) {
                self._progress = completedCount / totalCount;

            }
        } ,(err, resource) => {

            if (err) {
                console.log("err.."+err);
            } else {
                this.onLoadComplete();
            }
        });



    },

    onLoadComplete: function () {
        console.log("onLoadComplete" );
        this._isLoading = false;
        this._stateStr = "准备登陆";
        cc.director.loadScene("login");
        cc.loader.onComplete = null;
    },
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        if(this._stateStr.length == 0){
            return;
        }

        this.tipLabel.string = this._stateStr + ' ';
        if(this._isLoading){
            this.progressBar.progress=this._progress;

            this.tipLabel.string += Math.floor(this._progress * 100) + "%";
        }
        else{
            var t = Math.floor(Date.now() / 1000) % 4;
            for(var i = 0; i < t; ++ i){
                this.tipLabel.string += '.';
            }
        }
    }
});
